package com.grapecity.control.listview.item;

import java.util.ArrayList;

import com.grapecity.control.R;
import com.grapecity.control.gridview.CircleGridView;
import com.grapecity.control.gridview.ImageAdapter;
import com.grapecity.control.gridview.ImageAdapter.OnGridViewClickListener;
import com.grapecity.control.util.DownloadImageTask;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class CircleItemView extends FrameLayout implements OnClickListener, OnGridViewClickListener {
	private int position;
	private CircleItem circleItem;

	// show base info
	private ImageView avatarImg;
	private TextView name;

	// content
	private TextView content;

	// img
	private CircleGridView contentImgs;
	private ImageAdapter contentImgsAdapter;
	// private ImageView[] contentImgs = new ImageView[9];

	// link
	private ImageView linkImg;
	private TextView linkTxt;

	// time and more
	private TextView time;
	private View more;
	private PopupWindow morePopupWindow;
	private int mShowMorePopupWindowWidth;
	private int mShowMorePopupWindowHeight;

	// layout visibility
	private LinearLayout contentLayout;
	private FrameLayout imagesLayout;
	private LinearLayout linkLayout;
	private LinearLayout commentLayout;

	// interface
	public OnCommentListener commentListener;
	public OnItemClickListener itemClickListener;
	public OnGridViewClickListener gridViewClickListener;

	public CircleItemView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public CircleItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	// interface
	public interface OnCommentListener {
		void onComment(int position);

		void onLike(int position);
	}

	public interface OnItemClickListener {
		void onAvatarClick(int position);

		void onNameClick(int position);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();

		// layout
		this.contentLayout = (LinearLayout) findViewById(R.id.content_layout);
		this.imagesLayout = (FrameLayout) findViewById(R.id.images_layout);
		this.linkLayout = (LinearLayout) findViewById(R.id.link_layout);
		this.commentLayout = (LinearLayout) findViewById(R.id.comment_layout);
		// base info
		this.avatarImg = (ImageView) findViewById(R.id.imageHeader);
		this.name = (TextView) findViewById(R.id.textViewName);
		// content
		this.content = (TextView) findViewById(R.id.textViewMsg);
		// content images
		this.contentImgs = (CircleGridView) findViewById(R.id.images_gridview);
		this.contentImgsAdapter = new ImageAdapter(getContext());
		// link
		this.linkImg = (ImageView) findViewById(R.id.link_img);
		this.linkTxt = (TextView) findViewById(R.id.link_text);
		// mroe
		this.time = (TextView) findViewById(R.id.textTime);
		this.more = findViewById(R.id.imageButtonMore);
	}

	public void setCommentListener(OnCommentListener listener) {
		this.commentListener = listener;
	}

	public void setItemClickListener(OnItemClickListener listener) {
		this.itemClickListener = listener;
	}

	public void setGridViewClickListener(OnGridViewClickListener listener) {
		this.gridViewClickListener = listener;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public int getPosition() {
		return this.position;
	}

	public void setItemData(CircleItem circleItem) {
		this.circleItem = circleItem;
		updateItemData();
	}

	private void updateItemData() {
		name.setText(circleItem.getTxtName());
		name.setOnClickListener(this);

		avatarImg.setBackgroundColor(Color.rgb(128, 128, 128));
		avatarImg.setImageBitmap(null);
		avatarImg.setOnClickListener(this);
		new DownloadImageTask(avatarImg).execute(circleItem.getImgHeader());

		time.setText(circleItem.getTxtTime());
		if ((circleItem.getType() & CircleItem.TYPE_TXT) != 0) {
			contentLayout.setVisibility(View.VISIBLE);
			content.setText(circleItem.getContent());
		} else {
			contentLayout.setVisibility(View.GONE);
		}

		if ((circleItem.getType() & CircleItem.TYPE_TXT_IMG) != 0) {
			imagesLayout.setVisibility(View.VISIBLE);

			contentImgsAdapter.setItems(circleItem.getImgContent());
			contentImgsAdapter.setGridViewClickListener(this);
			contentImgs.setAdapter(contentImgsAdapter);
			contentImgs.setOnImageClickListener(contentImgsAdapter);
		} else {
			imagesLayout.setVisibility(View.GONE);
		}

		if ((circleItem.getType() & CircleItem.TYPE_TXT_LINK) != 0) {
			linkLayout.setVisibility(View.VISIBLE);
			linkImg.setImageBitmap(null);
			linkTxt.setText(circleItem.getLink());
		} else {
			linkLayout.setVisibility(View.GONE);
		}

		this.more.setOnClickListener(this);

		updateComment();
	}

	public void addComment() {
		updateComment();
	}

	private void updateComment() {
		if (circleItem.hasLike() || circleItem.hasComment()) {

			commentLayout.removeAllViews();
			commentLayout.setVisibility(View.VISIBLE);

			if (circleItem.hasLike()) {
				String like = "♡ ";
				int n = 0;
				for (String s : circleItem.getLikes()) {
					n++;
					like += s;
					if (n != circleItem.getLikes().size()) {
						like += ",";
					}
				}
				TextView t = new TextView(getContext());
				t.setLayoutParams(new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(
						ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)));
				t.setTextSize(16);
				t.setText(like);
				commentLayout.addView(t);
			}

			for (CircleComment c : circleItem.getComments()) {
				TextView t = new TextView(getContext());
				t.setLayoutParams(new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(
						ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)));
				t.setTextSize(16);
				t.setText(c.getComment());
				commentLayout.addView(t);
				t.setOnClickListener(this);
			}
		} else {
			commentLayout.setVisibility(View.GONE);
		}
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("InflateParams")
	private void showMore(View moreBtnView) {
		if (morePopupWindow == null) {
			LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View content = li.inflate(R.layout.item_more_layout, null, false);

			morePopupWindow = new PopupWindow(content, ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			morePopupWindow.setBackgroundDrawable(new BitmapDrawable());
			morePopupWindow.setOutsideTouchable(true);
			morePopupWindow.setTouchable(true);

			content.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
			mShowMorePopupWindowWidth = content.getMeasuredWidth();
			mShowMorePopupWindowHeight = content.getMeasuredHeight();

			View parent = morePopupWindow.getContentView();

			TextView like = (TextView) parent.findViewById(R.id.like);
			TextView comment = (TextView) parent.findViewById(R.id.comment);

			like.setOnClickListener(this);
			comment.setOnClickListener(this);
		}

		if (morePopupWindow.isShowing()) {
			morePopupWindow.dismiss();
		} else {
			int heightMoreBtnView = moreBtnView.getHeight();

			morePopupWindow.showAsDropDown(moreBtnView, -mShowMorePopupWindowWidth,
					-(mShowMorePopupWindowHeight + heightMoreBtnView) / 2);
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();

		if (id == R.id.imageButtonMore) {
			showMore(v);
		} else if (id == R.id.like) {
			if (commentListener != null) {
				commentListener.onLike(position);

				if (morePopupWindow != null && morePopupWindow.isShowing()) {
					morePopupWindow.dismiss();
				}
			}
		} else if (id == R.id.comment) {
			if (commentListener != null) {
				commentListener.onComment(position);

				if (morePopupWindow != null && morePopupWindow.isShowing()) {
					morePopupWindow.dismiss();
				}
			}
		} else if (id == R.id.imageHeader) {
			if (itemClickListener != null) {
				itemClickListener.onAvatarClick(position);
			}
		} else if (id == R.id.textViewName) {
			if (itemClickListener != null) {
				name.getText();
				itemClickListener.onNameClick(position);
			}
		}
	}

	@Override
	public void onGridViewCilcked(int position, ArrayList<String> items) {
		// TODO Auto-generated method stub
		if (this.gridViewClickListener != null) {
			this.gridViewClickListener.onGridViewCilcked(position, items);
		}
	}
}
